<template>
  <div
      style="height: 100vh; display: flex;  align-items: center; justify-content: center;background-color: #d74cef">
    <div style="display: flex;background-color: white;width: 60%;border-radius: 10px;overflow: hidden">
      <div style="flex: 1;display: flex;align-items: center;justify-content: center;">
        <img src="@/assets/register.jpg" alt="register" style="width: 60% ;height: 100%"/>
      </div>
      <div style="flex: 1;display: flex;align-items: center;justify-content: center;">
        <el-form :model="user" style="width: 80% " :rules="rules" ref="registerRef">
          <div style="font-size: 20px; font-weight: bold;text-align: center">欢迎注册后台管理系统</div>

          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" size="medium" v-model="user.username"
                      placeholder="请输入账号"></el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" size="medium" v-model="user.password"
                      placeholder="请输入密码"></el-input>
          </el-form-item>

          <el-form-item prop="confirmPassword">
            <el-input prefix-icon="el-icon-lock" size="medium" v-model="user.confirmPassword"
                      placeholder="请确认密码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="width: 100%" @click="login">注册</el-button>
          </el-form-item>
          <div style="display: flex">
            <div style="flex: 1">已有账号？请<span style="color: blue;cursor: pointer" @click="$router.push('/login')">登录</span></div>
            <div style="flex: 1;text-align:right"><span style="color: red; cursor: pointer" @click="$router.push('/forgetPassword')">忘记密码？</span></div>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>

import request from "@/utils/request";


export default {
  name: "Register",

  data() {
    // 验证码(错误)
    const validataPassword = (rule, confirmPassword, callback) => {
      if (confirmPassword === '') {
        callback(new Error('请确认密码'));
      } else if (confirmPassword !== this.user.password) {
        callback(new Error(''));
      } else {
        callback();
      }
    };

    return {
      user: {
        username: "",
        password: "",
        confirmPassword: "",
      },
      rules: {
        username: [
          {required: true, message: '请输入账号', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
        code: [
          {validator: validataPassword, trigger: 'blur'}// 自定义验证器
        ],
      }
    }
  },

  methods: {

    register() {
      this.$refs['registerRef'].validate((valid) => {
        if (valid) {
          request.post('/register', this.user).then(res => {
            if (res.code === 200) {
              this.$router.push('/login')
              this.$message.success('注册成功')
            } else {
              this.$message.error(res.msg)
            }
          });
        }
      });
    }

  }
}


</script>
<style scoped>
</style>
